﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Web.Services.Protocols;
using System.Web.Script.Services;

using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Text;

namespace Triax.Modules.Department
{
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    
    [ScriptService]
    public class WebService : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string InsertContractTemplateLog(string strThongKeId, string strMaSo, string strNum)
        {

            ContractTemplateLogController ctr = new ContractTemplateLogController();
            string strResult = "";
            string[] arr = strNum.Split(';');

            if (string.IsNullOrEmpty(strMaSo) == false)
            {
                ContractTemplateLog obj = new ContractTemplateLog();

                obj.ContractTemplateID = int.Parse(strThongKeId);
                obj.Type = int.Parse(strMaSo);
                obj.Publication = 1;
                obj.UserCreate = 1;
                obj.CreateDate = DateTime.Now;

                obj.FileDinhKem = arr[2];
                obj.GhiChu = arr[1];
                obj.TrangThai = 1;


                obj.InsertDate = DateTime.Now;
                obj.UpdateDate = DateTime.Now;
              //  AccountDB ac = AccountDB.GetAccountInfo(UsersUtility.Name);
                obj.CreateByUser = 1;// ac.ID;
                obj.UpdateByUser = 0;

                obj.IsDelete = 1;
                obj.tempInt1 = 1;
                obj.tempInt2 = 1;
                obj.tempInt3 = 1;
                obj.tempInt4 = 1;
                obj.tempStr1 = arr[0];  // tieu de
                obj.tempStr2 = "";
                obj.tempStr3 = "";
                obj.tempStr4 = "";
                obj.tempDate1 = DateTime.Now;
                obj.tempDate2 = DateTime.Now;
                ctr.AddContractTemplateLog(obj);


            }
            return "1";
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string SelectDetail(string strId, string strType)
        {
            string sJSON = "";
            string id = strId;
            string type = strType;
            if (strId != "0")
            {
                ContractTemplateLogController ctr = new ContractTemplateLogController();

              //  string cond = " AND type = " + strType + " AND ContractTemplateID = " + strId;
                string cond = " AND ContractTemplateID = " + strId;
                List<ContractTemplateLog> ds = ctr.GetContractTemplateLogsByCond(0,cond,"");
                JsonSerializer serializer0 = new JsonSerializer();
                serializer0.Converters.Add(new JavaScriptDateTimeConverter());
                serializer0.NullValueHandling = NullValueHandling.Ignore;
                for (int i = 0; i < ds.Count;i++ )
                {
                    try { 
                    ds[i].tempStr4 = "Y kiến " + ModuleDepartmentConst._DIC_DEPARTMENT_ROLESTR[ds[i].Type] + " (" +ds[i].InsertDate.ToString("dd/MM/yyyy hh:mm") + ")";
                    }
                    catch { }
                }
                sJSON = JsonConvert.SerializeObject(ds);
                return sJSON;
            }
            return sJSON;
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string SelectDetailP(string strId, string strType,string PID)
        {
            string sJSON = "";
            string id = strId;
            string type = strType;
            if (strId != "0")
            {
                ContractTemplateLogController ctr = new ContractTemplateLogController();

                //  string cond = " AND type = " + strType + " AND ContractTemplateID = " + strId;
                string cond = " AND ContractTemplateID = " + strId + " AND Publication in (" + PID + ") ";
                List<ContractTemplateLog> ds = ctr.GetContractTemplateLogsByCond(0, cond, "");
                JsonSerializer serializer0 = new JsonSerializer();
                serializer0.Converters.Add(new JavaScriptDateTimeConverter());
                serializer0.NullValueHandling = NullValueHandling.Ignore;
                List<ContractTemplateLog> ds1 = new List<ContractTemplateLog>();
                for (int i = 0; i < ds.Count; i++)
                {
                    try
                    {
                      //  ds[i].tempStr4 = "Y kiến " + ModuleDepartmentConst._DIC_DEPARTMENT_ROLESTR[ds[i].Type] + " (" + ds[i].InsertDate.ToString("dd/MM/yyyy hh:mm") + ")";
                        ds[i].tempStr4 = "Y kiến " + getUser(ds[i].CreateByUser) + " (" + ds[i].InsertDate.ToString("dd/MM/yyyy hh:mm") + ")";
                        if(ds[i].tempStr4.Trim().Length >5 && ds[i].tempStr1.Trim().Length>0)
                        {
                            ds1.Add(ds[i]);

                        }
                    }
                    catch { }
                }
                sJSON = JsonConvert.SerializeObject(ds1);
                return sJSON;
            }
            return sJSON;
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string SelectDetailPT(string strId, string strType, string PID)
        {
            string sJSON = "";
            string id = strId;
            string type = strType;
            if (strId != "0")
            {
                ContractTemplateLogController ctr = new ContractTemplateLogController();

                //  string cond = " AND type = " + strType + " AND ContractTemplateID = " + strId;
                string cond = " AND ContractTemplateID = " + strId + " AND Publication in (" + PID + ") " + " AND Type = " + type;
                List<ContractTemplateLog> ds = ctr.GetContractTemplateLogsByCond(0, cond, "");
                JsonSerializer serializer0 = new JsonSerializer();
                serializer0.Converters.Add(new JavaScriptDateTimeConverter());
                serializer0.NullValueHandling = NullValueHandling.Ignore;
                List<ContractTemplateLog> ds1 = new List<ContractTemplateLog>();
                for (int i = 0; i < ds.Count; i++)
                {
                    try
                    {
                        //  ds[i].tempStr4 = "Y kiến " + ModuleDepartmentConst._DIC_DEPARTMENT_ROLESTR[ds[i].Type] + " (" + ds[i].InsertDate.ToString("dd/MM/yyyy hh:mm") + ")";
                        ds[i].tempStr4 = "Y kiến " + getUser(ds[i].CreateByUser) + " (" + ds[i].InsertDate.ToString("dd/MM/yyyy hh:mm") + ")";
                        if (ds[i].tempStr4.Trim().Length > 5 && ds[i].tempStr1.Trim().Length > 0)
                        {
                            ds1.Add(ds[i]);

                        }
                    }
                    catch { }
                }
                sJSON = JsonConvert.SerializeObject(ds1); 
                return sJSON;
            }
            return sJSON;
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string SelectDetailPTIN(string strId, string strType, string PID)
        {
            string sJSON = "";
            string id = strId;
            string type = strType;
            if (strId != "0")
            {
                ContractTemplateLogController ctr = new ContractTemplateLogController();

                //  string cond = " AND type = " + strType + " AND ContractTemplateID = " + strId;
                string cond = " AND ContractTemplateID = " + strId + " AND Publication in (" + PID + ") " + " AND Type in( " + type + ") ";
                List<ContractTemplateLog> ds = ctr.GetContractTemplateLogsByCond(0, cond, "");
                JsonSerializer serializer0 = new JsonSerializer();
                serializer0.Converters.Add(new JavaScriptDateTimeConverter());
                serializer0.NullValueHandling = NullValueHandling.Ignore;
                List<ContractTemplateLog> ds1 = new List<ContractTemplateLog>();
                for (int i = 0; i < ds.Count; i++)
                {
                    try
                    {
                        //  ds[i].tempStr4 = "Y kiến " + ModuleDepartmentConst._DIC_DEPARTMENT_ROLESTR[ds[i].Type] + " (" + ds[i].InsertDate.ToString("dd/MM/yyyy hh:mm") + ")";
                        ds[i].tempStr4 = "Y kiến " + getUser(ds[i].CreateByUser) + " (" + ds[i].InsertDate.ToString("dd/MM/yyyy hh:mm") + ")";
                        if (ds[i].tempStr4.Trim().Length > 5 && ds[i].tempStr1.Trim().Length > 0)
                        {
                            ds1.Add(ds[i]);

                        }
                    }
                    catch { }
                }
                sJSON = JsonConvert.SerializeObject(ds1);
            }
            return sJSON;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string SelectDetail2(string strId, string strType)
        {
            string sJSON = "";
            string id = strId;
            string type = strType;
            if (strId != "0")
            {
                ContractTemplateLogController ctr = new ContractTemplateLogController();

                string cond = " AND tempInt4 = " + strType + " AND tempInt3 = " + strId;
               // string cond = " AND ContractTemplateID = " + strId;
                List<ContractTemplateLog> ds = ctr.GetContractTemplateLogsByCond(0, cond, "");
                JsonSerializer serializer0 = new JsonSerializer();
                serializer0.Converters.Add(new JavaScriptDateTimeConverter());
                serializer0.NullValueHandling = NullValueHandling.Ignore;
                for (int i = 0; i < ds.Count; i++)
                {
                    try
                    {
                        ds[i].tempStr4 = "Y kiến " + ModuleDepartmentConst._DIC_DEPARTMENT_ROLESTR[ds[i].Type] + " (" + ds[i].InsertDate.ToString("dd/MM/yyyy hh:mm") + ")";
                    }
                    catch { }
                }
                sJSON = JsonConvert.SerializeObject(ds);
                return sJSON;
            }
            return sJSON;
        }
        
        [WebMethod]
               public string InsertContractTemplateLog2(string strThongKeId, string strMaSo, string strNum)
        {

            ContractTemplateLogController ctr = new ContractTemplateLogController();
            string strResult = "";
            string[] arr = strNum.Split(';');

            if (string.IsNullOrEmpty(strMaSo) == false)
            {
                ContractTemplateLog obj = new ContractTemplateLog();
                obj.ModuleId = 510;
                obj.ContractTemplateID = int.Parse(strThongKeId);
                obj.Type = int.Parse(strMaSo);
                obj.Publication = 1;
                obj.UserCreate = 1;
                obj.CreateDate = DateTime.Now;

                obj.FileDinhKem = arr[2];
                obj.GhiChu = arr[1];
                obj.TrangThai = 1;


                obj.InsertDate = DateTime.Now;
                obj.UpdateDate = DateTime.Now;
                //  AccountDB ac = AccountDB.GetAccountInfo(UsersUtility.Name);
                obj.CreateByUser = 1;// ac.ID;
                obj.UpdateByUser = 0;

                obj.IsDelete = 0; // loại
                obj.tempInt1 = int.Parse(arr[3]); // loại: 1: KT, 2: Kinh doanh
                obj.tempInt2 = 0;
                obj.tempInt2 = 1;
                obj.tempInt3 = 1;
                obj.tempInt4 = 1;
                obj.tempStr1 = arr[0];  // tieu de
                obj.tempStr2 = "";
                obj.tempStr3 = "";
                obj.tempStr4 = "";
                obj.tempDate1 = DateTime.Now;
                obj.tempDate2 = DateTime.Now;
                 ctr.AddContractTemplateLog(obj);
                


            }
            return "1";
        }


        [WebMethod]
        public string InsertPublication(string strThongKeId, string strMaSo, string strNum)
        {
            //sNum = txtTenAnPham + txtTacgia + txtsotrang + txtkhuonkho + txtsoluongin + txtDongia + txtmasoISBN + txtTenCoSo + txtDiachi + txtsoKeHoach+ txtOther;
            PublicationController ctr = new PublicationController();
            string strResult = "";
            string[] arr = strNum.Split(';');
            int id = 0;
            //if(arr.Length>11)
            //{
            //    id = int.Parse(arr[12]); 
            //}

            

            if (string.IsNullOrEmpty(strMaSo) == false)
            {

                id = int.Parse(strMaSo);  // type
                if (id > 0)
                {
                    Publication obj = new Publication();
                    obj = ctr.GetPublication(0, id);
                    obj.ModuleId = 510;
                    obj.PublicationID = id;
                    obj.ContractTemplateID = int.Parse(strThongKeId);
                    obj.TenAnPham = arr[0];
                    obj.TacGia = arr[1];
                    obj.DichGia = "";
                    try
                    {
                        obj.SoTrang = int.Parse(arr[2]);
                    }
                    catch
                    {
                        obj.SoTrang = 0;
                    }
                    obj.KhoGiay = arr[3];
                    
                    try
                    {
                        obj.SoLuongIn = int.Parse(arr[4]); ;
                    }
                    catch
                    {
                        obj.SoLuongIn = 0;
                    }
                    
                    try
                    {
                        obj.DonGia = int.Parse(arr[5]); ;
                    }
                    catch
                    {
                        obj.DonGia = 0;
                    }
                    obj.TongTien = obj.DonGia * obj.SoLuongIn;
                    obj.MaSoISBN = arr[6];
                    obj.IsXuatBan = 0;
                    obj.IsPrint = 0;
                    obj.SoQuyetDinhXB = arr[9];
                    if (arr[16] != "")
                    {
                        obj.NgayQDXB = convertNgay(arr[16]);

                    }
                    else
                    {
                        obj.NgayQDXB = DateTime.Parse("01/01/1900");
                    }
                    obj.CreateByUser = int.Parse(arr[14]); ; ;// ac.ID;
                    obj.UpdateByUser = int.Parse(arr[13]); // nguoi bien tap
                    obj.TempStr1 = arr[7];
                    obj.TempStr2 = arr[8];
                    obj.TempStr3 = arr[10];
                    obj.TempStr4 = arr[11]; ;
                    if (id > 0)
                    {
                        ctr.UpdatePublication(obj);
                    }
                   
                }else
                {
                    Publication obj = new Publication();
                    obj.ModuleId = 510;
                    obj.PublicationID = id;
                    obj.ContractTemplateID = int.Parse(strThongKeId);
                    obj.TenAnPham = arr[0];
                    obj.TacGia = arr[1];
                    obj.DichGia = "";
                    try
                    {
                        obj.SoTrang = int.Parse(arr[2]);
                    }
                    catch
                    {
                        obj.SoTrang = 0;
                    }
                    obj.KhoGiay = arr[3];
                    obj.Bia = "";
                    try
                    {
                        obj.SoLuongIn = int.Parse(arr[4]); ;
                    }
                    catch
                    {
                        obj.SoLuongIn = 0;
                    }
                    obj.QuyCach = "";
                    obj.NoiDung = "";
                    obj.SoDangKy = "";
                    try
                    {
                        obj.DonGia = int.Parse(arr[5]); ;
                    }
                    catch
                    {
                        obj.DonGia = 0;
                    }
                    obj.TongTien = obj.DonGia * obj.SoLuongIn;
                    obj.MaSoISBN = arr[6];
                    obj.Noidungkhac = "";
                    obj.IsXuatBan = 0;
                    obj.IsPrint = 0;
                    obj.SoQuyetDinhXB = arr[9];
                    if (arr[16] != "")
                    {
                        obj.NgayQDXB = convertNgay(arr[16]);

                    }
                    else
                    {
                        obj.NgayQDXB = DateTime.Parse("01/01/1900");
                    }
                    obj.NguoiKy = "";
                    obj.SoXuatBan = arr[15];
                    obj.InsertDate = DateTime.Now;
                    obj.UpdateDate = DateTime.Now;
                    obj.CreateByUser = int.Parse(arr[14]); ; ;// ac.ID;
                    obj.UpdateByUser = int.Parse(arr[13]); // nguoi bien tap
                    obj.IsDelete = 0; // loại
                    obj.TempInt1 = 0;
                    obj.TempInt2 = 0;
                    obj.TempInt2 = 0;
                    obj.TempInt3 = 0;
                    obj.TempInt4 = 0;
                    obj.TempStr1 = arr[7];
                    obj.TempStr2 = arr[8];
                    obj.TempStr3 = arr[10];
                    obj.TempStr4 = arr[11]; ;
                    obj.TempDate1 = DateTime.Now;
                    obj.TempDate2 = DateTime.Now;
                   
                   
                        ctr.AddPublication(obj);
                   

                }
                
            }
            return "1";
        }

        [WebMethod]
        public string UpdatePublication(string strThongKeId, string strMaSo, string strNum)
        {
            //sNum = txtTenAnPham + txtTacgia + txtsotrang + txtkhuonkho + txtsoluongin + txtDongia + txtmasoISBN + txtTenCoSo + txtDiachi + txtsoKeHoach+ txtOther;
            PublicationController ctr = new PublicationController();
            string strResult = "";
            string[] arr = strNum.Split(';');

            if (string.IsNullOrEmpty(strMaSo) == false)
            {
                Publication obj = ctr.GetPublication(0,int.Parse(strMaSo));
             //   obj.PublicationID = int.Parse(strMaSo);
                obj.ContractTemplateID = int.Parse(strThongKeId);
                obj.TenAnPham = arr[0];
                obj.TacGia = arr[1];
              //  obj.DichGia = "";
                obj.SoTrang = int.Parse(arr[2]);
                obj.KhoGiay = arr[3];
              //  obj.Bia = "";
                obj.SoLuongIn = int.Parse(arr[4]); ;
             //   obj.QuyCach = "";
             //   obj.NoiDung = "";
             //   obj.SoDangKy = "";
                obj.DonGia = int.Parse(arr[5]); ;
                obj.TongTien = obj.DonGia * obj.SoLuongIn;
                obj.MaSoISBN = arr[6];
                obj.Noidungkhac = "";
                obj.IsXuatBan = 0;
                obj.IsPrint = 0;
                obj.SoQuyetDinhXB = arr[9];
                obj.NgayQDXB = DateTime.Parse("01/01/1900");
                obj.NguoiKy = "";
                obj.SoXuatBan = "";
             //   obj.InsertDate = DateTime.Now;
            //    obj.UpdateDate = DateTime.Now;
                obj.CreateByUser = 1;// ac.ID;
                obj.UpdateByUser = 0;
                obj.IsDelete = 0; // loại
                obj.TempInt1 = 0;
                obj.TempInt2 = 0;
                obj.TempInt2 = 0;
                obj.TempInt3 = 0;
                obj.TempInt4 = 0;
                obj.TempStr1 = arr[7];
                obj.TempStr2 = arr[8];
                obj.TempStr3 = arr[10];
             //   obj.TempStr4 = "";
             //   obj.TempDate1 = DateTime.Now;
            //    obj.TempDate2 = DateTime.Now;
                ctr.UpdatePublication(obj);
            }
            return "1";
        }

      

        [WebMethod]
        public string Deletenoidung(string strid)
        {

            if (string.IsNullOrEmpty(strid) == false)
            {
                PublicationController ctr = new PublicationController();
                ctr.DeletePublication(0,int.Parse(strid));

            }
            return "1";
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string SelectPublication(string strId, string strType)
        {
            string sJSON = "";
            string id = strId;
            string type = strType;
            PublicationController ctr = new PublicationController();

            string cond = " AND ContractTemplateID = " + strId;
            List<Publication> ds = ctr.GetPublicationsByCond(0,cond,"");
            for (int i = 0; i < ds.Count; i++)
            {
                ds[i].TempStr4 = ds[i].NgayQDXB.ToString("dd/MM/yyyy");

            }
            JsonSerializer serializer0 = new JsonSerializer();
            serializer0.Converters.Add(new JavaScriptDateTimeConverter());
            serializer0.NullValueHandling = NullValueHandling.Ignore;
            sJSON = JsonConvert.SerializeObject(ds);
            return sJSON;
        }


        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string SelectPublicationById(string strId, string strType)
        {
            string sJSON = "";
            string id = strId;
            string type = strType;
            PublicationController ctr = new PublicationController();

            string cond = " AND PublicationID = " + strId;
            List<Publication> ds = ctr.GetPublicationsByCond(0,cond,"");
            for(int i=0; i<ds.Count; i++)
            {
                ds[i].TempStr4 = ds[i].NgayQDXB.ToString("dd/MM/yyyy");

            }
            
            JsonSerializer serializer0 = new JsonSerializer();
            serializer0.Converters.Add(new JavaScriptDateTimeConverter());
            serializer0.NullValueHandling = NullValueHandling.Ignore;
            sJSON = JsonConvert.SerializeObject(ds);
            return sJSON;
        }
        public DateTime convertNgay(string ngaybatdau)
        {
            try
            {
                string[] dtemp = ngaybatdau.Split('/');
                if (dtemp.Length > 2)
                {
                    return new DateTime(int.Parse(dtemp[2]), int.Parse(dtemp[1]), int.Parse(dtemp[0]));
                }


            }
            catch
            {
                return DateTime.Parse("01/01/1900");
            }
            return DateTime.Parse("01/01/1900");
        }

        public string getUser(int id)
        {
            string res = "";
            DotNetNuke.Entities.Users.UserController objUserController = new DotNetNuke.Entities.Users.UserController();
            DotNetNuke.Entities.Users.UserInfo uu = objUserController.GetUser(0, id);
            if(uu !=null)
            {
                return uu.DisplayName;
            }
            return res;

          


        }
        public static string HtmlPhanTrang(int currPage, int totalRecord, int recordPerPage, string key)
        {
            int countPage;
            if (totalRecord % recordPerPage == 0)
                countPage = totalRecord / recordPerPage;
            else
                countPage = totalRecord / recordPerPage + 1;

            string html = "Tìm thấy (<b>" + totalRecord + ")</b> kết quả / " + countPage + " Trang. ";
            if (totalRecord <= recordPerPage)
            {
                //return html + "[1]";
                return html + "<input type='button' disabled='disabled' value='1' />";
            }

            if (countPage > 1)
            {
                if (currPage > 1)
                {
                    //html += "<a style='float:none' href='?page=1'>Trang Đầu</a>";
                    html += "<input type='button' class = 'button-submit' style='cursor: pointer;'  onclick= GetData(" + 1 + ",'" + "" + "'," + recordPerPage + ")  value ='Trang Đầu' />";
                }

                int begin = Math.Max(currPage - 5, 1);
                int end = Math.Min(currPage + 5, countPage);
                if (begin > 1)
                    html += "...";
                for (int i = begin; i <= end; i++)
                {
                    if (i == currPage)
                    {
                        html += "<input type='button' disabled='disabled' value='" + currPage + "' />";
                    }
                    else
                    {
                        //html += "<a style='float:none' href = '?page=" + i + "'>" + i + "</a>";
                        html += "<input type='button' style='cursor: pointer;'  onclick= GetData(" + i + ",'" + "" + "'," + recordPerPage + ")  value ='" + i + "' />";
                    }
                }
                if (end < countPage)
                    html += "....";
                if (currPage < countPage)
                {
                    //html += "<a style='float:none' href='?page=" + countPage + "'>Trang cuối</a>";
                    html += "<input type='button' style='cursor: pointer;'  onclick= GetData(" + countPage + ",'" + "" + "'," + recordPerPage + ")  value ='Trang Cuối' />";
                }
            }
            return html;
        }
        [WebMethod]
        public string searchWord3(string key, int page, int pagesize, string option)
        {
            List<DommainHosting> res1 = new List<DommainHosting>();
            StringBuilder mySqlString = new StringBuilder();
            string res ="";
            string cond = "";
            if (option == "1")
            {
                cond = " AND  TenNhaIn LIKE N'%" + fixsql(key) + "%' ";
            }
            if (option == "2")
            {
                //Diachi
                cond = " AND  Diachi LIKE N'%" + fixsql(key) + "%' ";
            }

            if (option == "3")
            {
                cond = " AND  SoDienThoai LIKE N'%" + fixsql(key) + "%' ";
            }

            if (option == "4")
            {
                cond = " AND  tempStr1 LIKE N'%" + fixsql(key) + "%' ";
            }
            if (option == "5")
            {
                cond = " AND  tempStr2 LIKE N'%" + fixsql(key) + "%' ";
            }

            
            //
            //
            //GetProductsByCond
            DommainHostingController dc = new DommainHostingController();
           // res1 = dc.GetDommainHostingsByCond_PhanTrang(page, 20, 20, cond, "");
         

            string cssclass = "row1";
            if (key.Length > 1)
            {
                //   res1 = dc.GetProductsByCond_Phantrang(page, 20, 20, cond, "");
                List<DommainHosting> res2 = dc.GetDommainHostingsByCond_count(1, cond, "");
               // res1 = dc.GetDommainHostingsByCond(0, cond, "");
                res1 = dc.GetDommainHostingsByCond_PhanTrang(page, 20, 20, cond, "");
                int count = res1.Count;
                res += "<br/><div class=\"div_result\" >Tìm thấy <b>(<font color='red'>" + res2[0].tempInt1 + ")</font></b> kết quả</div>";
                res += "<table  id=\"datalistproduct\" class=\"admindata\" style=' width:100%;' > \n";
                //res += "<thead><tr> <th scope=\"col\">STT</th>	<th scope=\"col\">Tên doanh nghiệp</th> <th scope=\"col\">Địa chỉ</th><th scope=\"col\">Tên sản phầm</th><th scope=\"col\">Số CN</th> <th scope=\"col\">Ngày ký</th> <th scope=\"col\">Xuất xứ</th>	</tr></thead> \n";
                res += "<thead><tr> 	<th scope=\"col\">Domain</th><th scope=\"col\">Chủ sở hữu </th> <th scope=\"col\">Tài khoản host</th> <th scope=\"col\">Tài khoản domain</th><th scope=\"col\">Tên công ty</th><th scope=\"col\">Thông tin máy chủ</th>	</tr></thead> \n";
                res += "<tbody>\n";
                res += "<tr>\n";
             //   string url = DotNetNuke.Common.Globals.NavigateURL(PortalSettings.ActiveTab.TabID, "Edit", "mid", ModuleId.ToString(), "DepartmentId", Eval("DommainHostingId").ToString());              
                for (int i = 0; i < res1.Count; i++)
                {
                    if (i < count)
                    {
                        DommainHosting p = res1[i];
                        if (i % 2 == 0)
                        {
                            cssclass = "row1";
                        }
                        else
                        {
                            cssclass = "row0";
                        }
                      //  if (option == "1")
                        {
                            res += "<tr class =\"" + cssclass + "\">\n";

                            res += "<td>" + p.TenNhaIn + "</td>";
                            res += "<td>" + p.Diachi + "</td>";
                            res += "<td>" + p.SoDienThoai + "</td>";
                            res += "<td>" + p.tempStr2 + "</td>";
                            res += "<td>" + p.tempStr1 + "</td>";
                            res += "<td>" + p.MST + "</td>";
                         
                        }
                    }
                }
                res += "<tfoot>\n";

                res += " <tr>";
                res += "<td colspan=\"9\">";
                   res += HtmlPhanTrang(page, res2[0].tempInt1, 20, key);
                res += "</td>";
                res += "</tfoot>\n";


                res += "</table>\n";
            }
            /*
            List<DommainHosting> productTypes = dc.GetProductTypesByCond(0, "", "");

            string res = "";
            if (key.Length > 2)
            {
                res1 = dc.GetProductsByCond_Phantrang(page, 20, 20, cond, "");
                List<DommainHosting> res2 = dc.GetProductsByCond_Count(1, cond, "");

                res += "<br/><div class=\"div_result\" >Tìm thấy <b>(<font color='red'>" + res2[0].CertifyNo + ")</font></b> kết quả</div>";
                res += "<table  id=\"datalistproduct\" class=\"admindata\" > \n";
                //res += "<thead><tr> <th scope=\"col\">STT</th>	<th scope=\"col\">Tên doanh nghiệp</th> <th scope=\"col\">Địa chỉ</th><th scope=\"col\">Tên sản phầm</th><th scope=\"col\">Số CN</th> <th scope=\"col\">Ngày ký</th> <th scope=\"col\">Xuất xứ</th>	</tr></thead> \n";
                res += "<thead><tr> <th scope=\"col\">STT</th>	<th scope=\"col\">Tên doanh nghiệp</th> <th scope=\"col\">Địa chỉ</th> <th scope=\"col\">Nhóm sản phẩm</th><th scope=\"col\">Tên sản phầm</th><th scope=\"col\">Số CN</th>	<th scope=\"col\">Ngày ký</th> <th scope=\"col\">Người ký</th><th scope=\"col\">Xuất xứ</th></tr></thead> \n";
                res += "<tbody>\n";
                res += "<tr>\n";


                //Extens
                int count = res1.Count;
                string condMain = " AND MainID in (0";
                for (int i = 0; i < res1.Count; i++)
                {
                    if (i < count)
                    {
                        condMain += "," + res1[i].MainID.ToString();
                    }
                }
                condMain += ") ";
                List<MainInfo> mains = new List<MainInfo>();
                mains = dc.GetMainsByCond(0, condMain, "");
                string cssclass = "";
                for (int i = 0; i < res1.Count; i++)
                {
                    if (i < count)
                    {
                        DommainHosting p = res1[i];
                        if (i % 2 == 0)
                        {
                            cssclass = "row1";
                        }
                        else
                        {
                            cssclass = "row0";
                        }
                        if (option == "1")
                        {
                            res += "<tr class =\"" + cssclass + "\">\n";
                            res += "<td>" + ((i + 1) + (page - 1) * pagesize) + "</td>";
                            res += "<td><a href=\"javascript:;\" onclick=\"timkiem2('" + getMainName(mains, res1[i].MainID) + "')\"> " + getMainName(mains, res1[i].MainID) + "</a></td>";
                            res += "<td>" + getMainAddress(mains, res1[i].MainID) + "</td>";
                            res += "<td>" + getProduceType(productTypes, res1[i].ProductTypeID) + "</td>";
                            res += "<td>" + Extensions.HighlightKeyWords(p.ProductName, key, "red", true) + "</td>";
                            res += "<td>" + p.CertifyNo + "/" + p.CreatedYear + "/-YT-CNTC</td>";
                            res += "<td>" + p.SignatureDate.ToString("dd/MM/yyyy") + "</td>";
                            res += "<td>" + p.SignatureName + "</td>";
                            res += "<td>" + p.SourceName + "</td>";
                            res += "</tr>\n";
                        }
                        if (option == "2")
                        {
                            res += "<tr class =\"" + cssclass + "\">\n";
                            res += "<td>" + ((i + 1) + (page - 1) * pagesize) + "</td>";
                            //  res += "<td>" + Extensions.HighlightKeyWords(getMainName(mains, res1[i].MainID), key, "red", true) + "</td>";
                            res += "<td><a href=\"javascript:;\" onclick=\"timkiem2('" + getMainName(mains, res1[i].MainID) + "')\"> " + Extensions.HighlightKeyWords(getMainName(mains, res1[i].MainID), key, "red", true) + "</a></td>";

                            res += "<td>" + getMainAddress(mains, res1[i].MainID) + "</td>";
                            res += "<td>" + getProduceType(productTypes, res1[i].ProductTypeID) + "</td>";
                            res += "<td>" + p.ProductName + "</td>";
                            res += "<td>" + p.CertifyNo + "/" + p.CreatedYear + "/-YT-CNTC</td>";
                            res += "<td>" + p.SignatureDate.ToString("dd/MM/yyyy") + "</td>";
                            res += "<td>" + p.SignatureName + "</td>";
                            res += "<td>" + p.SourceName + "</td>";
                            res += "</tr>\n";
                        }
                        if (option == "3")
                        {
                            res += "<tr class =\"" + cssclass + "\">\n";
                            res += "<td>" + ((i + 1) + (page - 1) * pagesize) + "</td>";

                            res += "<td><a href=\"javascript:;\" onclick=\"timkiem2('" + getMainName(mains, res1[i].MainID) + "')\"> " + Extensions.HighlightKeyWords(getMainName(mains, res1[i].MainID), key, "red", true) + "</a></td>";
                            res += "<td>" + Extensions.HighlightKeyWords(getMainAddress(mains, res1[i].MainID), key, "red", true) + "</td>";
                            res += "<td>" + getProduceType(productTypes, res1[i].ProductTypeID) + "</td>";
                            res += "<td>" + p.ProductName + "</td>";
                            res += "<td>" + p.CertifyNo + "/" + p.CreatedYear + "/-YT-CNTC</td>";
                            res += "<td>" + p.SignatureDate.ToString("dd/MM/yyyy") + "</td>";
                            res += "<td>" + p.SignatureName + "</td>";
                            res += "<td>" + p.SourceName + "</td>";
                            res += "</tr>\n";

                        }
                        if (option == "4")
                        {
                            res += "<tr class =\"" + cssclass + "\">\n";
                            res += "<td>" + ((i + 1) + (page - 1) * pagesize) + "</td>";

                            res += "<td><a href=\"javascript:;\" onclick=\"timkiem2('" + getMainName(mains, res1[i].MainID) + "')\"> " + Extensions.HighlightKeyWords(getMainName(mains, res1[i].MainID), key, "red", true) + "</a></td>";
                            res += "<td>" + Extensions.HighlightKeyWords(getMainAddress(mains, res1[i].MainID), key, "red", true) + "</td>";
                            res += "<td>" + getProduceType(productTypes, res1[i].ProductTypeID) + "</td>";
                            res += "<td>" + p.ProductName + "</td>";

                            res += "<td>" + Extensions.HighlightKeyWords(p.CertifyNo.ToString(), key, "red", true) + "/" + p.CreatedYear + "/-YT-CNTC</td>";
                            res += "<td>" + p.SignatureDate.ToString("dd/MM/yyyy") + "</td>";
                            res += "<td>" + p.SignatureName + "</td>";
                            res += "<td>" + p.SourceName + "</td>";
                            res += "</tr>\n";
                        }

                    }
             * */
                
             
               
             //   res += "<div >";
                //res += "<input class='styled-button-3' type='button' value='1' /> <input class='styled-button-3' type='button' value='2' /> <input class='styled-button-3' type='button' value='3' /> ";
            
          //     res += "</div >";
            
            return res;
            // return res1;
        }

        [WebMethod]
        public List<SourceInfo> getsource(string key, string option)
        {
            List<DommainHosting> res1 = new List<DommainHosting>();
            List<DommainHosting> res2 = new List<DommainHosting>();
            
            DommainHostingController dc = new DommainHostingController();
            List<SourceInfo> infos = new List<SourceInfo>();
            int count = 0;
            string cond = "";
            if (option == "1")
            {
                cond = " AND  TenNhaIn LIKE N'%" + (key) + "%' ";
                res1 = dc.GetDommainHostingsByCond(1, cond, "");
                for (int i = 0; i < res1.Count; i++)
                {
                    if (count < 10)
                    {
                        DommainHosting p = res1[i];
                        SourceInfo r = new SourceInfo();
                        r.SourceName = p.TenNhaIn;
                        infos.Add(r);
                        //     items.SetValue(p.ProductName, i);
                        res2.Add(p);
                    }
                    count++;
                }
                if (count > 0)
                    return infos;
            }

            if (option == "2")
            {
                cond = " AND  Diachi LIKE N'%" + (key) + "%' ";
                res1 = dc.GetDommainHostingsByCond(1, cond, "");
                for (int i = 0; i < res1.Count; i++)
                {
                    if (count < 10)
                    {
                        DommainHosting p = res1[i];
                        SourceInfo r = new SourceInfo();
                        r.SourceName = p.Diachi;
                        infos.Add(r);
                        //     items.SetValue(p.ProductName, i);
                        res2.Add(p);
                    }
                    count++;
                }
                if (count > 0)
                    return infos;
            }

            if (option == "3")
            {
                cond = " AND  SoDienThoai LIKE N'" + (key) + "%' ";
                res1 = dc.GetDommainHostingsByCond(1, cond, "");
                for (int i = 0; i < res1.Count; i++)
                {
                    if (count < 10)
                    {
                        DommainHosting p = res1[i];
                        SourceInfo r = new SourceInfo();
                        r.SourceName = p.SoDienThoai;
                        infos.Add(r);
                        //     items.SetValue(p.ProductName, i);
                        res2.Add(p);
                    }
                    count++;
                }
                if (count > 0)
                    return infos;
            }
            if (option == "4")
            {
                cond = " AND  tempStr1 LIKE N'" + (key) + "%' ";
                res1 = dc.GetDommainHostingsByCond(1, cond, "");
                for (int i = 0; i < res1.Count; i++)
                {
                    if (count < 10)
                    {
                        DommainHosting p = res1[i];
                        SourceInfo r = new SourceInfo();
                        r.SourceName = p.tempStr1;
                        infos.Add(r);
                        //     items.SetValue(p.ProductName, i);
                        res2.Add(p);
                    }
                    count++;
                }
                if (count > 0)
                    return infos;
            }
            if (option == "5")
            {
                cond = " AND  tempStr2 LIKE N'" + (key) + "%' ";
                res1 = dc.GetDommainHostingsByCond(1, cond, "");
                for (int i = 0; i < res1.Count; i++)
                {
                    if (count < 10)
                    {
                        DommainHosting p = res1[i];
                        SourceInfo r = new SourceInfo();
                        r.SourceName = p.tempStr2;
                        infos.Add(r);
                        //     items.SetValue(p.ProductName, i);
                        res2.Add(p);
                    }
                    count++;
                }
                if (count > 0)
                    return infos;
            }

            //
            //
            //GetProductsByCond



            //return new string[] { "1", "1", "1", "1", "1", "1", "1", "1" };
            return null;

            // return res1;
        }
        public string fixsql(string input)
        {
            string res = input.Replace("'", "\\'");
            // res = res.Replace("%", "\\%");
            res = res.Replace("\\", "\\\\");
            return res;

        }
      

    }
}
